<!doctype html>
<html lang="en">
 <head> 
  <meta charset="UTF-8"> 
  <meta name="description" content="燎火的个人博客"> 
  <meta name="keyword" content="燎火,qq799378666,软件开发,java,c#,js,html,html5,css"> 
  <title>如何正确使用Java异常处理机制</title> 
  <script src="/js/jquery-1.11.1.min.js"></script> 
  <script src="/js/bootstrap-3.3.5/js/bootstrap.js"></script> 
  <link href="/js/bootstrap-3.3.5/css/bootstrap.css" rel="stylesheet"> 
  <script src="/js/base.js"></script> 
  <script src="/js/nav.js"></script> 
  <script src="/js/jquery.toTop.min.js"></script> 
  <link href="/style/article.css" rel="stylesheet"> 
 </head> 
 <body> 
  <div id="main"> 
   <h3 id="title">如何正确使用Java异常处理机制</h3> 
   <div id="content">
    <p>本文的目标并不是介绍Java异常处理机制相关概念和语法，如果你有这方面的需求，请参考我的“&nbsp;<a href="http://leaforbook.com/blog/jexception/index.html" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration: none;"><span style="box-sizing: border-box; color: rgb(20, 113, 145);">Java异常 官方文档翻译系列</span></a>”文章。本文的目标是如何正确使用Java异常处理机制。</p>
    <p><span style="box-sizing: border-box; font-weight: 700;"><span style="box-sizing: border-box; color: rgb(20, 113, 145);"><span style="box-sizing: border-box; font-size: 19px;">第一节 异常处理概述</span></span></span></p>
    <p>在理想境界中，程序永远不会出现问题，用户输入的数据永远是正确的，逻辑没有任何问题 ，选择打开的文件也一定是存在的，内存永远是够用的……反正没有任何问题！但是一旦出现这些问题，如果处理不好，程序就不能正常运行了，用户就有可能再也不使用这个程序了。</p>
    <p>要处理异常，必先知道异常发生的原因；要知道异常发生的原因，必先知道异常发生的场景。你的程序可能和任何其他实体交互的时候，都可能发生异常。Java程序中，都是利用方法（Method）和其他实体进行交互。所以异常的发生、抛出、声明和处理都是在方法内。下图是Java程序可能和其他实体交互的概要图：</p>
    <p><span style="box-sizing: border-box; color: rgb(26, 144, 185);"><span style="box-sizing: border-box; font-weight: 700;">图1：你的方法与其他实体交互概要图</span></span></p>
    <p><img src="http://leaforbook.com/leaforheart.github.io/blog/jexception/image/5.png"></p>
    <p>如图1所示，你写的方法和外部实体交互大概可以分为五类：</p>
    <ol style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>和资源（Resource）交互，见图⑤处。这里资源的范围很广，比如进程外部的数据库，文件，SOA服务，其他各种中间件；进程内的类，方法，线程……都算是资源。</p></li>
     <li><p>给进程内的其他方法（User Method）提供服务，见图②处。</p></li>
     <li><p>依赖进程内的其他方法（Server Method），见图③处。包括Java平台提供的方法和其他第三方供应方提供的方法。</p></li>
     <li><p>和系统环境交互，见图⑧处。系统环境可能是直接环境——JVM，也可能是间接环境——操作系统或硬件等。</p></li>
     <li><p>给外部实体提供服务，见图①处。这种外部实体一般会通过容器（或其他类似的机制）和你的方法进行交互。所以，可以归为②，不予探讨。</p></li>
    </ol>
    <p>Java方法和每一类实体进行交互时，都可能发生异常。</p>
    <p>当和资源交互时，常常会因为资源不可用而发生异常，比如发生找不到文件、数据库连接错误、找不到类、找不到方法……等等状况。有可能是直接产生的，见图⑤处；有可能是间接产生的，比如图⑥处发生异常，Server Method把异常抛给Your Method，图③处就间接发生了异常。一般来说，你写的方法间接发生这类异常的可能性比直接发生要大得多，因为直接产生这类异常的方法在Java平台中已经提供了。对于这类异常，<span style="box-sizing: border-box; line-height: 1.45;">通常有以下几个特点：</span></p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>问题来自外部，你的方法本身的正常流程逻辑没问题。</p></li>
     <li><p>这类异常通常是暂时的，过段时间就可用了（经过资源维护者的彻夜奋战……）。最终用户通常也可以接受暂时的等待或采取替补方案。</p></li>
     <li><p>你的程序的其他功能还可以用。</p></li>
    </ul>
    <p>这时，你的方法应该这样处理：</p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>返回到一种安全状态，并能够让用户执行一些其他的命令（比如支付宝支付失败时返回一个弹出框，说明余额不足等原因，让用户重新选择其他支付渠道）；或者</p></li>
     <li><p>允许用户保存所有操作的结果，并以适当的方式终止程序（比如保存填了一半的表单）。</p></li>
    </ul>
    <p>然后，你应该协调各方，促进资源恢复可用，消除异常。</p>
    <p>当给用户方法（User Method ）提供服务时，用户可能会传入一些不合法的数据（或者其他不恰当的使用方法），进而对程序的正常流程造成破坏。你的方法应该检查每一个输入数据，如果发现不合法的数据，马上阻止执行流程，并通知用户方法。</p>
    <p>当调用服务方法（Server Method ）时，有可能会发生两类异常。一类是你的使用方法不正确，导致服务中止；一类是服务方法出了异常，然后传递给你的方法。如果是第一种异常，你应该检查并修改你的方法逻辑，消除BUG。对于第二类异常，你要么写一个处理器处理，要么继续传递给上层方法。</p>
    <p>当和系统环境交互时，有可能因为JVM参数设置不当，有可能因为程序产生了大量不必要的对象，也有可能因为硬故障（操作系统或硬件出了问题），导致整个程序不可用。当这类异常发生时，最终用户没法选择其他替代方案，操作到一半的数据会全部丢失。你的方法对这类异常一般没什么办法，既不能通过修改主流程逻辑来消除，也不能通过增加异常处理器来处理。所以通常你的方法对这类异常不需要做任何处理。但是你必须检查进程内的所有程序和系统环境是否正常，然后协调各方，修改BUG或恢复环境。</p>
    <p>Java的异常都是发生在方法内，所以研究Java异常，要以你设计的方法为中心。我们以“你的方法 ”为中心，总结一下处理办法：当服务方法告诉“你的方法 ”的主流程逻辑有问题时，就要及时修复BUG来消除异常；当用户方法非法使用“你的方法”时，应该直接中止主流程，并通知用户方法，强迫用户方法使用正确的方式，防止问题蔓延；当服务方法传递一个异常给“你的方法”时，你要判断“你的方法”是否合适处理这个异常，如果不合适，传递给上层方法，如果合适，写一个异常处理器处理这个异常。当系统环境出了问题，“你的方法”什么也做不了。</p>
    <p>刚才以“你的方法”为中心，总结了在“你的方法”内部的处理办法。现在以“你”为中心，总结一下方法外部的处理方法：当资源不可用的时候，你应该协调各方，恢复资源；当发生系统故障时，你应该协调各方，恢复系统。</p>
    <p>现在，已经基本分析清楚了异常发生的原因，以及相应的应对方法。下一节正式介绍Java异常处理机制。</p>
    <hr>
    <p><span style="box-sizing: border-box; font-weight: 700;"><span style="box-sizing: border-box; color: rgb(20, 113, 145);"><span style="box-sizing: border-box; font-size: 19px;">第二节 Java异常处理类</span></span></span></p>
    <p><span style="box-sizing: border-box; color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: medium;">Java把异常当做是破坏正常流程的一个事件，当事件发生后，就会触发处理机制。</span></p>
    <p>Java有一套独立的异常处理机制，在遇到异常时，方法并不返回任何值（返回值属于正常流程），而是抛出一个封装了错误信息的对象。下图是Java异常处理机制类层级结构图：</p>
    <p><span style="box-sizing: border-box; color: rgb(26, 144, 185);"><span style="box-sizing: border-box; font-weight: 700;">图2：Java异常处理机制类层级结构图</span></span></p>
    <p><img src="http://leaforbook.com/leaforheart.github.io/blog/jexception/image/6.png"></p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">2.1 Throwable</span></span></p>
    <p>所有的异常对象都派生于Throwable类的一个实例。</p>
    <p><span style="box-sizing: border-box; font-weight: 700;">2.1.1 Throwable有五种构造方法：</span></p>
    <table width="705">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="356"><p>Throwable()</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="348"><p>创建一个无详细信息的Throwable</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="356"><p>Throwable(String message)</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="348"><p>创建一个有详细信息的Throwable</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="356"><p>Throwable(String message, Throwable cause)</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="348"><p>创建一个有详细信息和发生原因的Throwable</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="356"><p><span style="box-sizing: border-box; color: rgb(227, 0, 0);">protected</span>&nbsp;Throwable(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="348"><p>创建一个有详细信息和发生原因的Throwable，并确定是否可以suppression，是否可以writable stack trace。<span style="box-sizing: border-box; color: rgb(227, 0, 0);">jdk7开始才有</span>。</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="356"><p>Throwable(Throwable cause)</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="348"><p>创建一个有发生原因的Throwable</p></td>
      </tr>
     </tbody>
    </table>
    <p>备注：</p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>suppression：被压抑的异常。想了解更多信息，请参看我的译文“<a href="http://leaforbook.com/blog/jexception/translate/tryResourceClose.html" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration: none;">try-with-resources语句</a>”。</p></li>
     <li><p>strack trace：堆栈跟踪。是一个方法调用过程列表，它包含了程序执行过程中方法调用的具体位置。</p></li>
    </ul>
    <p><span style="box-sizing: border-box; font-weight: 700;">2.1.2 Throwable的所有成员方法：</span></p>
    <table width="705">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public final void addSuppressed(Throwable exception)</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>把指定的异常加入当前异常的suppressed异常列表，这样就可以把这个异常传递下去。这个方法是线程安全的，通常被try-with-resources语句调用（可以说是专为这种新语句设计的）。<span style="box-sizing: border-box; color: rgb(227, 0, 0);">jdk7开始才有</span>。</p><p>如果enableSuppression为false，这个方法无效</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public Throwable fillInStackTrace()</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>填充这个执行堆栈跟踪，这个方法把当前线程的堆栈帧的当前状态记录到了这个Throwable对象信息里面。并返回当前Throwable实例。<span style="box-sizing: border-box; color: rgb(227, 0, 0);">构造器方法都会首先调用这个方法</span>。</p><p>如果writableStackTrace为false，这个方法无效。</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public Throwable getCause()</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>获取cause Throwable信息。其实就是获取底层的异常信息。对应于initCause方法。</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public String getLocalizedMessage()</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>对于当前Throwable，创建一个本地化描述。供子类重写。如果子类没有重写这个方法，这个方法返回和getMessage()一样。</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public String getMessage()</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>返回当前Throwable的详细信息。</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public StackTraceElement[] getStackTrace()</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>获取堆栈跟踪信息，可以通过程序遍历StackTraceElement对象，获取个性化的信息。StackTraceElement的toString方法可以返回标准的堆栈跟踪信息。</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public final Throwable[] getSuppressed()&nbsp;</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>对应于addSuppressed方法，获取suppressed异常列表。这个方法是线程安全的，通常被try-with-resources语句调用（可以说是专为这种新语句设计的）。<span style="box-sizing: border-box; color: rgb(227, 0, 0);">jdk7开始才有</span>。如果没有，就会返回一个空数组。</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public Throwable initCause(Throwable cause)</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>设置引起当前Throwable被抛出的Throwable。只能设置一次cause Throwable，通常在构造方法就设置好了，或者在创建Throwable实例以后马上调用本方法。</p><p>try {<br>&nbsp; &nbsp; lowLevelOp();<br>} catch (LowLevelException le) {<br>&nbsp; &nbsp; throw (HighLevelException)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new HighLevelException().initCause(le); // Legacy constructor</p><p>}</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public void printStackTrace()</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>把这个Throwable和它的堆栈跟踪信息打印到标准的错误字节流里面</p><p><span style="box-sizing: border-box; color: rgb(227, 0, 0);">把信息注入流以后，可以继续处理，分析并存储到不同的介质中：</span></p><p>Throwable t = new Throwable();</p><p>ByteArrayOutStream out = new ByteArrayOutStream();</p><p>t.printStackTrace();</p><p>String desc = out.toString();</p><p>//这个例子其实是public void printStackTrace(PrintStream s)的，放在一处解释</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public void printStackTrace(PrintStream s)</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>把这个Throwable和它的堆栈跟踪信息打印到指定的打印字节流里面</p><p><span style="box-sizing: border-box; color: rgb(227, 0, 0);">同上</span></p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public void printStackTrace(PrintWriter s)</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>把这个Throwable和它的堆栈跟踪信息打印到指定的打印字符流里面</p><p><span style="box-sizing: border-box; color: rgb(227, 0, 0);">同上</span></p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public void setStackTrace(StackTraceElement[] stackTrace)</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>手动设置堆栈跟踪信息。这个方法是给RPC框架或其他先进系统设计的，允许客户端覆盖默认的由fillInStackTrace()生成的默认堆栈跟踪信息，如果这个Throwable是从一个序列化字节流读取而来的话。</p><p>如果writableStackTrace为false，这个方法无效。</p></td>
      </tr>
      <tr style="box-sizing: border-box;">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="208"><p>public String toString()</p></td>
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="496"><p>返回当前Throwable的<span style="box-sizing: border-box; color: rgb(227, 0, 0);">简短</span>描述。</p></td>
      </tr>
     </tbody>
    </table>
    <p>备注：</p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>所有派生于Throwable类的异常类，基本都没有这些成员方法，也就是说所有的异常类都只是一个标记，记录发生了什么类型的异常（通过标记，编译期和JVM做不同的处理），所有实质性的行为Throwable都具备了。</p></li>
     <li><p>综上，在一个Throwable里面可以获取什么信息？</p></li>
     <ul style="list-style-type: square;" class=" list-paddingleft-2">
      <li><p>获取堆栈跟踪信息（源代码中哪个类，哪个方法，第几行出现了问题……从当前代码到最底层的代码调用链都可以查出来）</p></li>
      <li><p>获取引发当前Throwable的Throwable。追踪获取底层的异常信息。</p></li>
      <li><p>获取被压抑了，没抛出来的其他Throwable。一次只能抛出一个异常，如果发生了多个异常，其他异常就不会被抛出，这时可以通过加入suppressed异常列表来解决（JDK7以后才有）。</p></li>
      <li><p>获取基本的详细描述信息</p></li>
     </ul>
    </ul>
    <p>从图2可以看出，Throwable类只有两个直接继承者：Error和Exception。然后Exception又分为RuntimeException和Checked Exception。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">2.2 Error</span></span></p>
    <p>在Java中， 由系统环境问题引起的异常，一般都继承于Error类。</p>
    <p>对于Error类：</p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>一般开发者不要自定义Error子类，因为它代表系统级别的错误。与一般的程序无关。</p></li>
     <li><p>在Java异常处理机制中，Error不强制捕获或声明，也就是不强制处理。因为程序本身对此类错误无能为力。一般情况下我们只要把堆栈跟踪信息记录下来就行。</p></li>
    </ul>
    <p>下列是Java平台中直接继承于Error的错误类型：</p>
    <table width="869" style="width: 768px;">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="868"><p><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/annotation/AnnotationFormatError.html" title="class in java.lang.annotation" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">AnnotationFormatError</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/AssertionError.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">AssertionError</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/AWTError.html" title="class in java.awt" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">AWTError</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/nio/charset/CoderMalfunctionError.html" title="class in java.nio.charset" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">CoderMalfunctionError</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/parsers/FactoryConfigurationError.html" title="class in javax.xml.parsers" style="box-sizing: border-box; color: rgb(187, 122, 42); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">FactoryConfigurationError</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/stream/FactoryConfigurationError.html" title="class in javax.xml.stream" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">FactoryConfigurationError</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/io/IOError.html" title="class in java.io" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">IOError</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/LinkageError.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">LinkageError</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/ServiceConfigurationError.html" title="class in java.util" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ServiceConfigurationError</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/ThreadDeath.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ThreadDeath</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/transform/TransformerFactoryConfigurationError.html" title="class in javax.xml.transform" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">TransformerFactoryConfigurationError</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/VirtualMachineError.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">VirtualMachineError</a></p></td>
      </tr>
     </tbody>
    </table>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">2.3 Exception</span></span></p>
    <p>在Java中，除了系统环境问题引起的异常，一般都继承于Exception类。Exception分为RuntimeException和Checked Exception。Checked Exception必须要捕获或声明。而RuntimeException不强制。</p>
    <p>对于Exception类：</p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>如果你创建了一个异常类型，直接继承于Exception，那么这个异常类型将属于检查异常（Checked Exception）。</p></li>
    </ul>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">2.4 RuntimeException</span></span></p>
    <p>在Java中，由于接口方法使用不当造成的异常，一般属于RuntimeException，也就是运行时异常。</p>
    <p>对于RuntimeException：</p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>如果你调用服务方法的方式不正确，你应该马上修改代码，避免发生RuntimeException</p></li>
     <li><p>如果是用户方法调用你的方法的方式不正确，你应该立刻抛出RuntimeException，强制让使用者修正代码或改变使用方式，防止问题蔓延</p></li>
     <li><p>一般情况下，不要捕获或声明RuntimeException。因为问题在于你的程序本身有问题，如果你用异常流程处理了，反而让正常流程问题一直存在</p></li>
    </ul>
    <p>下列是Java平台中直接继承于RuntimeException的运行时异常：</p>
    <table width="869" style="width: 768px;">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="868"><p><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/annotation/AnnotationTypeMismatchException.html" title="class in java.lang.annotation" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">AnnotationTypeMismatchException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/ArithmeticException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ArithmeticException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/ArrayStoreException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ArrayStoreException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/nio/BufferOverflowException.html" title="class in java.nio" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">BufferOverflowException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/nio/BufferUnderflowException.html" title="class in java.nio" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">BufferUnderflowException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/swing/undo/CannotRedoException.html" title="class in javax.swing.undo" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">CannotRedoException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/swing/undo/CannotUndoException.html" title="class in javax.swing.undo" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">CannotUndoException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/ClassCastException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ClassCastException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/color/CMMException.html" title="class in java.awt.color" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">CMMException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/ConcurrentModificationException.html" title="class in java.util" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ConcurrentModificationException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/bind/DataBindingException.html" title="class in javax.xml.bind" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">DataBindingException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/org/w3c/dom/DOMException.html" title="class in org.w3c.dom" style="box-sizing: border-box; color: rgb(187, 122, 42); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">DOMException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/EmptyStackException.html" title="class in java.util" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">EmptyStackException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/EnumConstantNotPresentException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">EnumConstantNotPresentException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/org/w3c/dom/events/EventException.html" title="class in org.w3c.dom.events" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">EventException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/nio/file/FileSystemAlreadyExistsException.html" title="class in java.nio.file" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">FileSystemAlreadyExistsException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/nio/file/FileSystemNotFoundException.html" title="class in java.nio.file" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">FileSystemNotFoundException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/IllegalArgumentException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">IllegalArgumentException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/IllegalMonitorStateException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">IllegalMonitorStateException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/geom/IllegalPathStateException.html" title="class in java.awt.geom" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">IllegalPathStateException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/IllegalStateException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">IllegalStateException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/IllformedLocaleException.html" title="class in java.util" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">IllformedLocaleException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/image/ImagingOpException.html" title="class in java.awt.image" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ImagingOpException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/annotation/IncompleteAnnotationException.html" title="class in java.lang.annotation" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">IncompleteAnnotationException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/IndexOutOfBoundsException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">IndexOutOfBoundsException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/management/JMRuntimeException.html" title="class in javax.management" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">JMRuntimeException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/org/w3c/dom/ls/LSException.html" title="class in org.w3c.dom.ls" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">LSException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/reflect/MalformedParameterizedTypeException.html" title="class in java.lang.reflect" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">MalformedParameterizedTypeException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/lang/model/type/MirroredTypesException.html" title="class in javax.lang.model.type" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">MirroredTypesException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/MissingResourceException.html" title="class in java.util" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">MissingResourceException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/NegativeArraySizeException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">NegativeArraySizeException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/NoSuchElementException.html" title="class in java.util" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">NoSuchElementException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/crypto/NoSuchMechanismException.html" title="class in javax.xml.crypto" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">NoSuchMechanismException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/NullPointerException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">NullPointerException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/color/ProfileDataException.html" title="class in java.awt.color" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ProfileDataException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/security/ProviderException.html" title="class in java.security" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ProviderException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/nio/file/ProviderNotFoundException.html" title="class in java.nio.file" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ProviderNotFoundException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/image/RasterFormatException.html" title="class in java.awt.image" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">RasterFormatException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/concurrent/RejectedExecutionException.html" title="class in java.util.concurrent" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">RejectedExecutionException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/SecurityException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">SecurityException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/org/omg/CORBA/SystemException.html" title="class in org.omg.CORBA" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">SystemException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/bind/TypeConstraintException.html" title="class in javax.xml.bind" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">TypeConstraintException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/TypeNotPresentException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">TypeNotPresentException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/reflect/UndeclaredThrowableException.html" title="class in java.lang.reflect" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">UndeclaredThrowableException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/lang/model/UnknownEntityException.html" title="class in javax.lang.model" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">UnknownEntityException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/print/attribute/UnmodifiableSetException.html" title="class in javax.print.attribute" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">UnmodifiableSetException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/UnsupportedOperationException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">UnsupportedOperationException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/ws/WebServiceException.html" title="class in javax.xml.ws" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">WebServiceException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/invoke/WrongMethodTypeException.html" title="class in java.lang.invoke" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">WrongMethodTypeException</a></p></td>
      </tr>
     </tbody>
    </table>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">2.5 Checked Exception</span></span></p>
    <p>在Java中，直接或间接因为“资源”问题引起的异常，一般属于检查异常（Checked Exception） 。检查异常继承于Exception，而不继承于RuntimeException。</p>
    <p>对于检查异常：</p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>必须捕获或声明</p></li>
     <li><p>交给关心这个异常的方法处理</p></li>
     <li><p>异常处理器应该引导用户接下来怎么办，至少做到安全退出</p></li>
    </ul>
    <p>下列是Java平台中直接继承于Exception的检查异常：</p>
    <table width="869" style="width: 768px;">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="868"><p><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/security/acl/AclNotFoundException.html" title="class in java.security.acl" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">AclNotFoundException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/rmi/activation/ActivationException.html" title="class in java.rmi.activation" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ActivationException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/rmi/AlreadyBoundException.html" title="class in java.rmi" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">AlreadyBoundException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/org/omg/CORBA/portable/ApplicationException.html" title="class in org.omg.CORBA.portable" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ApplicationException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/AWTException.html" title="class in java.awt" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">AWTException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/prefs/BackingStoreException.html" title="class in java.util.prefs" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">BackingStoreException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/management/BadAttributeValueExpException.html" title="class in javax.management" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">BadAttributeValueExpException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/management/BadBinaryOpValueExpException.html" title="class in javax.management" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">BadBinaryOpValueExpException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/swing/text/BadLocationException.html" title="class in javax.swing.text" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">BadLocationException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/management/BadStringOperationException.html" title="class in javax.management" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">BadStringOperationException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/concurrent/BrokenBarrierException.html" title="class in java.util.concurrent" style="box-sizing: border-box; color: rgb(187, 122, 42); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">BrokenBarrierException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/security/cert/CertificateException.html" title="class in javax.security.cert" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">CertificateException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/CloneNotSupportedException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">CloneNotSupportedException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/zip/DataFormatException.html" title="class in java.util.zip" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">DataFormatException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/datatype/DatatypeConfigurationException.html" title="class in javax.xml.datatype" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">DatatypeConfigurationException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/security/auth/DestroyFailedException.html" title="class in javax.security.auth" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">DestroyFailedException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/concurrent/ExecutionException.html" title="class in java.util.concurrent" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ExecutionException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/swing/tree/ExpandVetoException.html" title="class in javax.swing.tree" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ExpandVetoException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/FontFormatException.html" title="class in java.awt" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">FontFormatException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/security/GeneralSecurityException.html" title="class in java.security" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">GeneralSecurityException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/org/ietf/jgss/GSSException.html" title="class in org.ietf.jgss" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">GSSException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/instrument/IllegalClassFormatException.html" title="class in java.lang.instrument" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">IllegalClassFormatException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/InterruptedException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">InterruptedException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/beans/IntrospectionException.html" title="class in java.beans" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">IntrospectionException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/management/InvalidApplicationException.html" title="class in javax.management" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">InvalidApplicationException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/sound/midi/InvalidMidiDataException.html" title="class in javax.sound.midi" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">InvalidMidiDataException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/prefs/InvalidPreferencesFormatException.html" title="class in java.util.prefs" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">InvalidPreferencesFormatException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/management/modelmbean/InvalidTargetObjectTypeException.html" title="class in javax.management.modelmbean" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">InvalidTargetObjectTypeException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/io/IOException.html" title="class in java.io" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">IOException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/bind/JAXBException.html" title="class in javax.xml.bind" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">JAXBException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/management/JMException.html" title="class in javax.management" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">JMException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/crypto/KeySelectorException.html" title="class in javax.xml.crypto" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">KeySelectorException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/security/acl/LastOwnerException.html" title="class in java.security.acl" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">LastOwnerException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/sound/sampled/LineUnavailableException.html" title="class in javax.sound.sampled" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">LineUnavailableException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/crypto/MarshalException.html" title="class in javax.xml.crypto" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">MarshalException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/sound/midi/MidiUnavailableException.html" title="class in javax.sound.midi" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">MidiUnavailableException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/datatransfer/MimeTypeParseException.html" title="class in java.awt.datatransfer" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">MimeTypeParseException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/activation/MimeTypeParseException.html" title="class in javax.activation" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">MimeTypeParseException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/naming/NamingException.html" title="class in javax.naming" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">NamingException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/geom/NoninvertibleTransformException.html" title="class in java.awt.geom" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">NoninvertibleTransformException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/rmi/NotBoundException.html" title="class in java.rmi" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">NotBoundException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/security/acl/NotOwnerException.html" title="class in java.security.acl" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">NotOwnerException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/text/ParseException.html" title="class in java.text" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ParseException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/parsers/ParserConfigurationException.html" title="class in javax.xml.parsers" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ParserConfigurationException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/print/PrinterException.html" title="class in java.awt.print" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">PrinterException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/print/PrintException.html" title="class in javax.print" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">PrintException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/security/PrivilegedActionException.html" title="class in java.security" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">PrivilegedActionException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/beans/PropertyVetoException.html" title="class in java.beans" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">PropertyVetoException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/ReflectiveOperationException.html" title="class in java.lang" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ReflectiveOperationException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/security/auth/RefreshFailedException.html" title="class in javax.security.auth" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">RefreshFailedException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/org/omg/CORBA/portable/RemarshalException.html" title="class in org.omg.CORBA.portable" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">RemarshalException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span>&nbsp;<a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/org/xml/sax/SAXException.html" title="class in org.xml.sax" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">SAXException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/script/ScriptException.html" title="class in javax.script" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ScriptException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/rmi/server/ServerNotActiveException.html" title="class in java.rmi.server" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">ServerNotActiveException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/soap/SOAPException.html" title="class in javax.xml.soap" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">SOAPException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/sql/SQLException.html" title="class in java.sql" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">SQLException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/concurrent/TimeoutException.html" title="class in java.util.concurrent" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">TimeoutException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/util/TooManyListenersException.html" title="class in java.util" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">TooManyListenersException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/transform/TransformerException.html" title="class in javax.xml.transform" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">TransformerException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/crypto/dsig/TransformException.html" title="class in javax.xml.crypto.dsig" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">TransformException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/lang/instrument/UnmodifiableClassException.html" title="class in java.lang.instrument" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">UnmodifiableClassException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/sound/sampled/UnsupportedAudioFileException.html" title="class in javax.sound.sampled" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">UnsupportedAudioFileException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/security/auth/callback/UnsupportedCallbackException.html" title="class in javax.security.auth.callback" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">UnsupportedCallbackException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/awt/datatransfer/UnsupportedFlavorException.html" title="class in java.awt.datatransfer" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">UnsupportedFlavorException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/swing/UnsupportedLookAndFeelException.html" title="class in javax.swing" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">UnsupportedLookAndFeelException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/crypto/URIReferenceException.html" title="class in javax.xml.crypto" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">URIReferenceException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/java/net/URISyntaxException.html" title="class in java.net" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">URISyntaxException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/org/omg/CORBA/UserException.html" title="class in org.omg.CORBA" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">UserException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/transaction/xa/XAException.html" title="class in javax.transaction.xa" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">XAException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/management/modelmbean/XMLParseException.html" title="class in javax.management.modelmbean" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">XMLParseException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/crypto/dsig/XMLSignatureException.html" title="class in javax.xml.crypto.dsig" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">XMLSignatureException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/stream/XMLStreamException.html" title="class in javax.xml.stream" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">XMLStreamException</a><span style="box-sizing: border-box; color: rgb(53, 56, 51); font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">,&nbsp;</span><a href="http://tool.oschina.net/uploads/apidocs/jdk_7u4/javax/xml/xpath/XPathException.html" title="class in javax.xml.xpath" style="box-sizing: border-box; color: rgb(76, 107, 135); text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size: 12.16px;">XPathException</a></p></td>
      </tr>
     </tbody>
    </table>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">2.6 Uncheck Exception</span></span></p>
    <p>Error和RuntimeException统称为非检查异常。两者的共同点就是都不被强制捕获或声明。实际上两者描述问题的范围完全没有交集。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">2.7 总结</span></span></p>
    <p>所有的功能都在Throwable类里面实现了，子类只需要直接继承或间接继承它，并且加上需要的构造方法就行（一般而言，第一第二个构造方法是必须的，也可以全部加上），而且构造方法通常只需要一行代码：super(...)，也就是说只要调用父类的构造方法就行了。Java把异常分为三类（Error，Checked Exception，RuntimeException），只是在语法层面上有不同的标记而已。它们自身拥有的功能一样，运行时系统处理它们的方式也是一样的（你也可以捕获或声明非检查异常），不同的是编译器对它们的区别对待（检查异常必须要在代码里处理，非检查异常就不需要），以及程序员对它们的区别对待（这需要程序员遵循良好的实践原则）。</p>
    <p>这三类异常全部覆盖了第一节中所描述的异常发生场景，图1中，④⑤⑥处可能会发生Checked Exception，②③处既可能会发生RuntimeException也可能会发生Checked Exception，⑦⑧⑨处可能会发生Error。①处已经超出了Java异常处理机制的范畴（这属于容器要考虑的问题），通常在数据中加入返回码来通知异常信息。</p>
    <p>理解了每一类异常对应的场景，很多人其实已经知道该怎么用了，不必往下看了。</p>
    <hr>
    <p><span style="box-sizing: border-box; color: rgb(20, 113, 145);"><span style="box-sizing: border-box; font-weight: 700;"><span style="box-sizing: border-box; font-size: 19px;">第三节 Java异常处理执行流程探究</span></span></span></p>
    <p>首先设计两个方法，一个方法可能会抛出RuntimeException，一个方法可能会抛出Checked Exception。</p>
    <p>public String runtimeServerMethod(String s) {</p>
    <p>&nbsp; if(s==null) {</p>
    <p>&nbsp; &nbsp;&nbsp; throw new RuntimeException("runtimeServerMethod方法的字符串不能为空");</p>
    <p>&nbsp; }</p>
    <p>&nbsp; return s;</p>
    <p>}</p>
    <p>private BufferedReader bufferedReader;</p>
    <p>public String checkedServerMethod(String s) throws IOException {</p>
    <p>&nbsp;&nbsp; File file = new File(s);</p>
    <p>&nbsp;&nbsp; Reader reader = new FileReader(file);</p>
    <p>&nbsp; &nbsp;bufferedReader = new BufferedReader(reader);</p>
    <p>&nbsp;&nbsp; String result = bufferedReader.readLine();</p>
    <p>&nbsp;&nbsp; return result;</p>
    <p>}</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">3.1 流程一</span></span></p>
    <p>public void currentMethod() {</p>
    <p>&nbsp; System.out.println("--------------------try-catch-before");</p>
    <p>&nbsp; String result = this.runtimeServerMethod(null);</p>
    <p>&nbsp; System.out.println("--------------------result:"+result);</p>
    <p>&nbsp; System.out.println("--------------------try-catch-after");</p>
    <p>}</p>
    <p>执行结果：</p>
    <table width="869" style="width: 768px;">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="868"><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------try-catch-before</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">Exception in thread "main"</span></span>&nbsp;<span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">java.lang.RuntimeException</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">: runtimeServerMethod方法的字符串不能为空</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.runtimeServerMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:34</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.currentMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:26</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.userMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:20</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.main(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:16</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p></td>
      </tr>
     </tbody>
    </table>
    <p>分析：</p>
    <p>违反了runtimeServerMethod方法的使用规则——入参不能为null，导致产生了一个运行时异常。主流程线程直接中断，后面的代码不再执行。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">3.2 流程二</span></span></p>
    <p>public void currentMethod() {</p>
    <p>&nbsp; System.out.println("--------------------try-catch-before");</p>
    <p>&nbsp; String result = null;</p>
    <p>&nbsp; try {</p>
    <p>&nbsp; &nbsp;&nbsp; result = this.runtimeServerMethod(null);</p>
    <p>&nbsp; &nbsp;&nbsp; System.out.println("--------------------result:"+result);</p>
    <p>&nbsp; } catch (Exception e) {</p>
    <p>&nbsp; &nbsp; &nbsp;System.out.println("--------------------in-catch");</p>
    <p>&nbsp; }</p>
    <p>&nbsp; System.out.println("--------------------try-catch-after");</p>
    <p>}</p>
    <p>执行结果：</p>
    <table width="869" style="width: 768px;">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="868"><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------try-catch-before</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------in-catch</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------try-catch-after</span></span></p></td>
      </tr>
     </tbody>
    </table>
    <p>分析：</p>
    <p>RuntimeException也可以捕获处理（<span style="box-sizing: border-box; color: rgb(227, 0, 0);">这是一种不好的实践</span>），运行时系统并不会区分异常类型。异常发生以后，try代码块后面的代码不再执行，而是跳到catch代码块，线程不中断，执行完整个方法。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">3.3 流程三</span></span></p>
    <p>public void currentMethod() {</p>
    <p>&nbsp;&nbsp; System.out.println("--------------------try-catch-before");</p>
    <p>&nbsp;&nbsp; String result = null;</p>
    <p>&nbsp;&nbsp; result = this.runtimeServerMethod("Conform to the rules");</p>
    <p>&nbsp;&nbsp; System.out.println("--------------------result:"+result);</p>
    <p>&nbsp;&nbsp; System.out.println("--------------------try-catch-after");</p>
    <p>}</p>
    <p>执行结果：</p>
    <table width="869" style="width: 768px;">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="868"><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------try-catch-before</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------result:Conform to the rules</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------try-catch-after</span></span></p></td>
      </tr>
     </tbody>
    </table>
    <p>分析：</p>
    <p>当符合服务方法的规则时，就不会抛出运行时异常。方法就可以正常执行完成。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">3.4 流程四</span></span></p>
    <p>public void currentMethod() {</p>
    <p>&nbsp; &nbsp;&nbsp; System.out.println("--------------------try-catch-before");</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;String result = null;</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;try {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = this.checkedServerMethod("");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------result:"+result);</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------in-catch");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} finally {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------in-finally");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(bufferedReader!=null) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bufferedReader.close();</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp; &nbsp;&nbsp; System.out.println("--------------------try-catch-after");</p>
    <p>}</p>
    <p>执行结果：</p>
    <table width="869" style="width: 768px;">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="868"><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------try-catch-before</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------in-catch</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------in-finally</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------try-catch-after</span></span></p></td>
      </tr>
     </tbody>
    </table>
    <p>分析：</p>
    <p>当调用了checkedServerMethod方法，并且发生了Checked Exception时，一定要捕获或声明该异常，否则编译不通过。上例中，异常发生后，try代码块后面的代码不再执行，跳到catch代码块，再执行finally代码块（在这里有关闭资源的操作），然后再执行其余部分。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">3.5 流程五</span></span></p>
    <p>public void currentMethod() {</p>
    <p>&nbsp; &nbsp;&nbsp; System.out.println("--------------------try-catch-before");</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;String result = null;</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;try {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = this.checkedServerMethod("");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------result:"+result);</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------in-catch");</p>
    <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} finally {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------in-finally");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(bufferedReader!=null) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bufferedReader.close();</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp; &nbsp;&nbsp; System.out.println("--------------------try-catch-after");</p>
    <p>&nbsp;}</p>
    <p>执行结果：</p>
    <table width="869" style="width: 768px;">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="868"><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------try-catch-before</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------in-catch</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------in-finally</span></span></p></td>
      </tr>
     </tbody>
    </table>
    <p>分析：</p>
    <p>和流程四不同之处在于，catch代码块多了一条return语句。执行结果也相应发生了变化，try-catch-finally代码块后面的代码不再执行。而且值得注意的是，finally代码块的代码依然执行了。这就是finally代码块的意义。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">3.6 流程六</span></span></p>
    <p>public void userMethod() {</p>
    <p>&nbsp; &nbsp; try {</p>
    <p>&nbsp; &nbsp; &nbsp; &nbsp; this.currentMethod();</p>
    <p>&nbsp; &nbsp; } catch (IOException e) {</p>
    <p>&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();</p>
    <p>&nbsp; &nbsp; }</p>
    <p>}</p>
    <p>public void currentMethod() throws IOException {</p>
    <p>&nbsp; &nbsp;&nbsp; System.out.println("--------------------try-catch-before");</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;String result = null;</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;try {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = this.checkedServerMethod("");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------result:"+result);</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------in-catch");</p>
    <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.checkedServerMethod("catch");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} finally {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------in-finally");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(bufferedReader!=null) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bufferedReader.close();</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------try-catch-after");</p>
    <p>}</p>
    <p>执行结果：</p>
    <table width="869" style="width: 768px;">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="868"><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------try-catch-before</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------in-catch</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------in-finally</span></span></p><p><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">java.io.FileNotFoundException</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">: catch (系统找不到指定的文件。)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileInputStream.open0(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">Native Method</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileInputStream.open(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">FileInputStream.java:195</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileInputStream.&lt;init&gt;(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">FileInputStream.java:138</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileReader.&lt;init&gt;(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">FileReader.java:72</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.checkedServerMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:62</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.currentMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:38</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.userMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:23</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.main(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:18</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p></td>
      </tr>
     </tbody>
    </table>
    <p>分析：</p>
    <p>和流程五类似，只不过catch代码块不是返回一个正常值，而是抛出一个Checked Exception。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">3.7 流程七</span></span></p>
    <p>public void userMethod() {</p>
    <p>&nbsp; try {</p>
    <p>&nbsp; &nbsp; &nbsp; this.currentMethod();</p>
    <p>&nbsp; } catch (IOException e) {</p>
    <p>&nbsp; &nbsp; &nbsp; e.printStackTrace();</p>
    <p>&nbsp; }</p>
    <p>}</p>
    <p>public void currentMethod() throws IOException {</p>
    <p>&nbsp; &nbsp;&nbsp; System.out.println("--------------------try-catch-before");</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;String result = null;</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;try {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = this.checkedServerMethod("");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------result:"+result);</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------in-catch");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;this.checkedServerMethod("catch");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} finally {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------in-finally");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(bufferedReader!=null) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bufferedReader.close();</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.checkedServerMethod("finally");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp; &nbsp; &nbsp; System.out.println("--------------------try-catch-after");</p>
    <p>}</p>
    <p>执行结果：</p>
    <table width="869" style="width: 768px;">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="868"><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------try-catch-before</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------in-catch</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------in-finally</span></span></p><p><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">java.io.FileNotFoundException</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">: finally (系统找不到指定的文件。)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileInputStream.open0(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">Native Method</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileInputStream.open(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">FileInputStream.java:195</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileInputStream.&lt;init&gt;(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">FileInputStream.java:138</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileReader.&lt;init&gt;(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">FileReader.java:72</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.checkedServerMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:63</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.currentMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:48</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.userMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:23</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.main(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:18</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p></td>
      </tr>
     </tbody>
    </table>
    <p>分析：</p>
    <p>和流程六比起来，流程七在finally代码块也抛出了一个异常，最终在userMethod方法里面捕获到的是finally代码块的异常，catch代码块里抛出的异常被压抑了。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">3.8 流程八</span></span></p>
    <p>public void userMethod() {</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;try {</p>
    <p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; this.currentMethod();</p>
    <p>&nbsp; &nbsp;&nbsp; } catch (IOException e) {</p>
    <p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; e.printStackTrace();</p>
    <p>&nbsp; &nbsp;&nbsp; }</p>
    <p>}</p>
    <p>public void currentMethod() throws IOException {</p>
    <p>&nbsp; &nbsp;&nbsp; System.out.println("--------------------try-catch-before");</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;String result = null;</p>
    <p>&nbsp;&nbsp;&nbsp; &nbsp;try {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = this.checkedServerMethod("");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------result:"+result);</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}finally {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("--------------------in-finally");</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(bufferedReader!=null) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bufferedReader.close();</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>&nbsp; &nbsp;&nbsp; System.out.println("--------------------try-catch-after");</p>
    <p>&nbsp;}</p>
    <p>执行结果：</p>
    <table width="869" style="width: 768px;">
     <tbody style="box-sizing: border-box;">
      <tr style="box-sizing: border-box;" class="firstRow">
       <td style="box-sizing: border-box; padding-top: 10px; padding-bottom: 10px; border-color: rgb(211, 211, 211); margin: 0px;" width="868"><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------try-catch-before</span></span></p><p><span style="font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">--------------------in-finally</span></span></p><p><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">java.io.FileNotFoundException</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">:</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileInputStream.open0(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">Native Method</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileInputStream.open(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">FileInputStream.java:195</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileInputStream.&lt;init&gt;(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">FileInputStream.java:138</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.io.FileReader.&lt;init&gt;(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">FileReader.java:72</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.checkedServerMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:59</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.currentMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:34</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.userMethod(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:23</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p><p><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.leaforbook.javaexception.App.main(</span></span><span style="color:#0066CC;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;"><span style="text-decoration:underline;">App.java:18</span></span></span><span style="color:#FF0000;font-family:Courier New"><span style="box-sizing: border-box; font-size: 12pt;">)</span></span></p></td>
      </tr>
     </tbody>
    </table>
    <p>分析：</p>
    <p>如果没有catch代码块，强制要求声明抛出异常。userMethod捕获到的是try代码块抛出的异常（如果finally代码块也抛出异常，这个异常就会被压抑）。finally代码块还是会执行。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">3.9 总结</span></span></p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>在运行时环境，并不会区分异常的类型，所以程序员自己要遵从良好的实践原则，否则Java异常处理机制就会被误用。</p></li>
     <li><p>然后finally代码块总是会在方法返回或方法抛出异常前执行，而try-catch-finally代码块后面的代码就有可能不会再执行。</p></li>
     <li><p>finally代码块里面不推荐使用return语句或throw语句。</p></li>
     <li><p>try代码块一定要求要有一个catch代码块或finally代码块（二者取其一就行）。</p></li>
     <li><p>catch处理器的优先级比声明异常语句要高。</p></li>
     <li><p>如果多处抛出异常，finally代码块里面的异常会压抑其他异常。</p></li>
    </ul>
    <hr>
    <p><span style="box-sizing: border-box; font-weight: 700;"><span style="box-sizing: border-box; color: rgb(20, 113, 145);"><span style="box-sizing: border-box; font-size: 19px;">第四节 Java异常处理实践原则</span></span></span></p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">4.1 使用异常，而不使用返回码</span></span></p>
    <p>关于这一点，在我的译文“<a href="http://leaforbook.com/blog/jexception/translate/advantages.html" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration: none;">使用异常的优势</a>”有很详细的描述。理解了这一点，程序员们才会想要使用Java异常处理机制。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">4.2 利用运行时异常设定方法使用规则</span></span></p>
    <p>很常见的例子就是，某个方法的参数不能为空。在实践中，很多程序员的处理方式是，当传入的这个参数为空的时候，就返回一个特殊值（最常见的就是返回一个null，让用户方法决定怎么办）。还有的处理方式是，自己给一个默认值去兼容这种不合法参数，自己决定怎么办。这两种实践都是不好的。</p>
    <p>对于第一种处理方式，返回值是用来处理正常流程的，如果用来处理异常流程，就会让用户方法的正常流程变复杂。一次调用可能不明显，当有多个连续调用就会变得很复杂了。对于第二种处理方式，看起来很强大，因为“容错”能力看起来很强，有些程序员甚至可能会为此沾沾自喜。但是它也一样让正常流程变复杂了，这不是最糟糕的，最糟糕的是，你不知道下一次用户会出什么鬼点子，传个你现有处理代码处理不了的东西进来。这样你又得加代码，继续变复杂……BUG就是这样产生的。</p>
    <p>好的实践方式就是，设定方法的使用规则，遇到不合法的使用方式时，立刻抛出一个运行时异常。这样既不会让主流程代码变复杂，也不会制造不必要的BUG。为什么是运行时异常而不是检查异常呢？这是为了强迫用户修改代码或者改正使用方式——这属于用户的使用错误。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">4.3 消除运行时异常</span></span></p>
    <p>当你的程序发生运行时异常，通常都是因为你使用别人的方法的方式不正确（如果设计这个异常的人设计错误，就另当别论。比如设计者捕获一个检查异常，然后在处理器抛出一个运行时异常给用户。如果遇上这样的供应商，还是弃用吧）。所以，一般都是采取修改代码的方式，而不是新增一个异常流程。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">4.4 正确处理检查异常</span></span></p>
    <p>处理检查异常的时候，处理器一定要做到下面的要求才算合格：</p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>返回到一种安全状态，并能够让用户执行一些其他的命令；或者</p></li>
     <li><p>允许用户保存所有操作的结果，并以适当的方式终止程序。</p></li>
    </ul>
    <p>不好的实践案例一：因为有的异常发生的概率很小，有些程序员就会写出下面的代码：</p>
    <p>public Image loadImage(String s) {</p>
    <p>&nbsp; &nbsp; &nbsp;try {</p>
    <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code...</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (Exception e)</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{}</p>
    <p>&nbsp; &nbsp; &nbsp;code2...</p>
    <p>}</p>
    <p>catch代码块里面什么都不写！或者只在里面打一个log。这样既不会传递到上层方法，又不会报编译错误，还不用动脑筋……</p>
    <p>不好的实践案例二：捕获一个检查异常，什么都不做（或只打一个log），然后抛出一个运行时异常：</p>
    <p>public Image loadImage(String s) {</p>
    <p>&nbsp; &nbsp; &nbsp;try {</p>
    <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code...</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (Exception e){</p>
    <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeExcepiton();</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p>}</p>
    <p>这样也不会让上层方法感觉到这个异常的存在，也不会报编译错误了，也不用动什么脑筋……</p>
    <p>在案例一中，一旦出现了异常，try代码块里的代码没执行完，用户要求做的事情没做完，却又没有任何反馈或者得到一个错误反馈。</p>
    <p>在案例二中，一旦出现了异常，try代码块里的代码没执行完，虽然把运行时异常抛给用户了，用户也不会去处理这个异常，又没有办法通过改变使用方式消除异常，直接让用户代码崩溃掉。</p>
    <p>对于检查异常，好的实践方式是：</p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>让可以处理这个异常的方法去处理。衡量的标准就是在你这个方法写一个处理器，这个处理器能不能做到本节开头的那两个要求，如果不能，就往上抛。如果你不能知道所有用户的所有需求，你通常就做不到那两个要求。</p></li>
     <li><p>有必要的时候可以通过链式异常包装一下，再抛出。</p></li>
     <li><p>最终的处理器一定要做到本节开头的那两个要求。</p></li>
    </ul>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">4.5 使主流程代码保持整洁</span></span></p>
    <p>一个try代码块后面可以跟多个catch代码块，这就让一些可能会发生不同异常的代码可以写在一块，让代码看起来很清晰。相反，在一个方法里写多个try-catch，或者写嵌套的try-catch，就会让主流程代码变得很混乱。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">4.6 使用try-with-resources</span></span></p>
    <p>请参看我的译文“<a href="http://leaforbook.com/blog/jexception/translate/tryResourceClose.html" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration: none;">try-with-resources语句</a>”。</p>
    <p>try-with-resources语句比起普通的try语句，干净整洁的多。而且最终抛出的异常是正常流程中抛出的异常。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">4.7 尽量处理最具体的异常</span></span></p>
    <p>尽量使用最具体的异常类作为处理器匹配的类型。这样处理器就不用兼顾很多种情形，不易出错。从Java7开始，一个处理器可以处理多种异常类型。</p>
    <p>注意：同一个try语句中，比较具体的异常的catch代码块应写在前面，比较通用的异常的catch代码块应写在后面。</p>
    <p><span style="box-sizing: border-box; font-size: 16px;"><span style="box-sizing: border-box; font-weight: 700;">4.8 设计自己的异常类型要遵循的原则</span></span></p>
    <p>当你是一个模块开发者，你就很有必要设计一组或多组自己的异常类型。一般情况下，要遵守如下原则：</p>
    <ul style="box-sizing: border-box; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal; background-color: rgb(255, 255, 255);" class=" list-paddingleft-2">
     <li><p>确定什么场景下，需要创建自己的异常类型：（参看我的译文“<a href="http://leaforbook.com/blog/jexception/translate/creating.html" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration: none;">创建异常类</a>”）。</p></li>
     <li><p>为你的接口方法的使用规则创建一组运行时异常。</p></li>
     <li><p>包装别人的检查异常的时候，一定也要用检查异常。这样异常才能传递给上层方法处理。</p></li>
     <li><p>设计一组有层次结构的异常，而不是设计一堆零零散散的异常。</p></li>
     <li><p>区分清楚异常发生的原因，然后决定你的异常是检查异常还是运行时异常。</p></li>
     <li><p>模块内部不需要处理自己定义的异常。</p></li>
    </ul>
    <hr>
    <p>Java异常处理机制的目的至少有三个：一是归类处理不同的异常，二是提供足够的信息方便调试，三是让主流程代码保持整洁。</p>
    <p><br></p>
   </div> 
  </div>  
 </body>
</html>